<template>
  <div class="home">
    <div class="site-content animate">
      <!--通知栏-->
      <div class="notify">
        <quote style="border-left: 3px solid green; margin-top: 10px" >
          类型:
            <button v-for="item in typeList"  @click="fetchListType(item.name)" :value=item.name :key="item.id" :style="{'backgroundColor':item.color}">
                <span>{{item.name}}</span>
            </button>
          
        </quote>
        <!-- <quote style="border-left: 3px solid gray; margin-top: 10px">
          排序:
          <button style="background-color: #7093DB;">时间</button>
          <button style="background-color: #9370DB ">访问量</button>
          <button style="background-color: #9370DB ">评论数</button>
        </quote> -->
      </div>
      <!--文章列表-->
      <main class="site-main" :class="{ search: hideSlogan }">
        <section-title><b>学习记录</b></section-title>
        <template v-for="item in postList">
          <post :post="item" :key="item.id"></post>
        </template>
      </main>

      <!--加载更多-->
      <!-- <div class="more" v-show="hasNextPage">
        <div class="more-btn" @click="loadMore">加载更多</div>
      </div> -->
    </div>
  </div>
</template>

<script>
import Banner from "@/components/banner";
import Feature from "@/components/feature";
import sectionTitle from "@/components/section-title";
import Post from "@/components/post";
import SmallIco from "@/components/small-ico";
import Quote from "@/components/quote";
import { fetchFocus, fetchListType ,getAllType} from "../api";
import { type } from "os";

export default {
  name: "Home",
  props: ["cate", "words"],
  data() {
    return {
      features: [],
      postList: [],
      currPage: 0,
      hasNextPage: false,
      typeList:[],
      typeName:"",
    };
  },
  components: {
    Banner,
    Feature,
    sectionTitle,
    Post,
    SmallIco,
    Quote,
  },
  computed: {
    searchWords() {
      return this.$route.params.words;
    },
    category() {
      return this.$route.params.cate;
    },
    hideSlogan() {
      return this.category || this.searchWords;
    },
    notice() {
      return this.$store.getters.notice;
    },
  },
  methods: {
    fetchFocus() {
      fetchFocus()
        .then((res) => {
          this.features = res.data || [];
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 文章列表
    fetchListType(typeNameValue) {
        this.currPage = 0
        this.typeName = typeNameValue
        fetchListType(this.currPage, 300,typeNameValue)
        .then((res) => {
          this.postList = res.data.content || [];
          this.currPage = res.data.pageable.pageNumber;
          this.hasNextPage = !res.data.empty;
          this.typeName = res.data.content.typeName
        })
        .catch((err) => {
          console.log(err);
        });
        this.getAllType();
    },  
    
    // 类型
    getAllType() {
        getAllType()
        .then((res) => {
          this.typeList = res.data || [];
          
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 加载更多（分页）
    loadMore() {
        fetchListType(this.currPage + 1, 3,this.typeName).then((res) => {
        this.postList = this.postList.concat(res.data.content || []);
        this.currPage = res.data.pageable.pageNumber;
        this.hasNextPage = !res.data.empty;
      });
      this.getAllType();
    },
  },
  mounted() {
    this.fetchFocus();
  },
  created() {
    this.fetchListType("");
    this.getAllType();

  },
};
</script>
<style scoped lang="less">
button {
  width: auto;
  height: 35px;
  color: #fff;
  text-align: center;
  background-color: #8fd0cc;
  color: #fff;
  line-height: 50%;
  border-radius: 8px;
  border: 0;
  outline: 0;
//   transition: background 0.2s;
  padding: 1em;
  margin-right: 15px;
  margin-bottom: 10px;

}
button:hover {
  background: #666;
}

.site-content {
  .notify {
    margin: 90px 0;
    border-radius: 3px;
    & > div {
      padding: 20px;
    }
  }

  .search-result {
    padding: 15px 20px;
    text-align: center;
    font-size: 20px;
    font-weight: 400;
    border: 1px dashed #ddd;
    color: #828282;
  }
}

.top-feature {
  width: 100%;
  height: auto;
  margin-top: 30px;

  .feature-content {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    position: relative;

    .feature-item {
      width: 32.9%;
    }
  }
}

.site-main {
  padding-top: 1px;

  &.search {
    padding-top: 0;
  }
}

.more {
  margin: 50px 0;
  .more-btn {
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #adadad;
    border: 1px solid #adadad;
    border-radius: 20px;
    margin: 0 auto;
    cursor: pointer;
    &:hover {
      color: #8fd0cc;
      border: 1px dashed #8fd0cc;
    }
  }
}

/******/
@media (max-width: 800px) {
  .top-feature {
    display: none;
  }

  .site-main {
    padding-top: 40px;
  }

  .site-content {
    .notify {
      margin: 30px 0 0 0;
    }

    .search-result {
      margin-bottom: 20px;
      font-size: 16px;
    }
  }
}

/******/
</style>
